package com.quectel.cms.controller.forum;


import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.business.common.forum.ForumTopicBusiness;
import com.quectel.cms.controller.BaseController;
import com.quectel.cms.util.SessionHolder;
import com.quectel.constant.core.forum.ForumConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.forum.dto.ForumTopicDto;
import com.quectel.core.module.forum.dto.mongo.ForumTopicCountDto;
import com.quectel.core.module.forum.service.ForumTopicService;
import com.quectel.core.module.forum.service.mongo.ForumTopicCountService;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author bob
 * @email bob.yu@quectel.com
 * @date 2023/05/10 11:21
 */
@RestController
@RequestMapping("forumTopicCount")
@Tag(name = "ForumTopicController", description = "邻友圈话题统计相关api")
public class ForumTopicCountController extends BaseController {

    @DubboReference
    private ForumTopicService forumTopicService;
    @DubboReference
    private VillageService villageService;
    @DubboReference
    private ForumTopicCountService forumTopicCountService;
    @Autowired
    private ForumTopicBusiness forumTopicBusiness;


    @GetMapping("list")
    @RequirePermissions(values = "forumTopicCount:list")
    @Operation(summary = "查询所有的话题列表")
    public Response<Page<ForumTopicCountDto>> queryList(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize,
            @Parameter(description = "城市Code") @RequestParam(required = false) String cityCode,
            @Parameter(description = "小区Id") @RequestParam(required = false) Long villageId,
            @Parameter(description = "话题类别") @RequestParam(value = "category", required = false) Byte category
    ) {

        Request request = Request.configParams(Request.parseParams(httpServletRequest));
        //topic的一些限制
        HashMap<String, Object> topicParams = new HashMap<>(2);
        topicParams.put(LambdaUtil.getFieldName(ForumTopicDto::getStatus), String.valueOf(SystemConstants.NORMAL));
        if (category != null) {
            topicParams.put(LambdaUtil.getFieldName(ForumTopicDto::getCategory), String.valueOf(category));
        }

        List<ForumTopicDto> forumTopicDtos = new ArrayList<>();
        //根据不同的范围查询topicList
        if(villageId!=null){
            forumTopicDtos = forumTopicBusiness.getVillageTopic(Collections.singletonList(villageId), topicParams);
        }else if(StringUtils.isNotBlank(cityCode)){
            forumTopicDtos = forumTopicBusiness.getCityTopic(SessionHolder.getSysUser().getTenantId(), cityCode, topicParams);
        }else {
            forumTopicDtos = forumTopicBusiness.getAll(SessionHolder.getSysUser().getTenantId(), topicParams);
        }

        if (CollectionUtils.isEmpty(forumTopicDtos)) {
            return Response.<Page<ForumTopicCountDto>>ok().wrap(Page.empty(request.getPageNo(), request.getPageSize()));
        }

        List<ForumTopicCountDto> forumTopicCountDtos = null;

        //查询话题数量
        request.put(ForumConstants.TOPICS, forumTopicDtos.stream().map(ForumTopicDto::getId).toArray());
        request.put(SystemConstants.SORT_ORDER_BY_KEY, LambdaUtil.getFieldName(ForumTopicCountDto::getPostCount));
        request.put(SystemConstants.SORT_ORDER_RULE_KEY, SystemConstants.SORT_ORDER_RULE_DESC);

        int total = forumTopicCountService.queryTotal(request.getParams());
        if (total > 0) {

            forumTopicCountDtos = forumTopicCountService.queryList(request.getParams());
            forumTopicCountDtos.forEach(forumTopicCountDto -> {

                ForumTopicDto forumTopicDto = forumTopicService.selectCacheById(forumTopicCountDto.getTopicId());
                forumTopicCountDto.setForumTopic(forumTopicDto);
            });
        } else {
            forumTopicCountDtos = new ArrayList<>();
        }

        Page<ForumTopicCountDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, forumTopicCountDtos);
        return Response.<Page<ForumTopicCountDto>>ok().wrap(page);
    }


}
